package com.dingreading.cloud.ssxcx.service.impl;

import com.dingreading.cloud.common.util.PageUtil;
import com.dingreading.cloud.ssxcx.dto.XcxStudentLearningProgressDto;
import com.dingreading.cloud.ssxcx.entity.XcxStudentHomework;
import com.dingreading.cloud.ssxcx.entity.XcxStudentLearningProgress;
import com.dingreading.cloud.ssxcx.entity.table.XcxStudentLearningProgressTableDef;
import com.dingreading.cloud.ssxcx.mapper.XcxStudentLearningProgressMapper;
import com.dingreading.cloud.ssxcx.service.XcxStudentLearningProgressService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * 小程序学生学习进度表 服务层实现。
 *
 * @author EDY
 * @since 2025-08-18
 */
@Service
public class XcxStudentLearningProgressServiceImpl extends BaseServiceImpl<XcxStudentLearningProgressMapper, XcxStudentLearningProgress, XcxStudentLearningProgressTableDef> implements XcxStudentLearningProgressService {

    @Override
    protected XcxStudentLearningProgressTableDef getTable() {
        return XcxStudentLearningProgressTableDef.xcxStudentLearningProgress;
    }

    @Override
    public XcxStudentLearningProgress getLatest(String studentUid, String subjectLessonUid, Integer materialStage) {
        QueryCondition condition = QueryCondition.createEmpty()
                .and(table.studentUid.eq(studentUid))
                .and(table.subjectLessonUid.eq(subjectLessonUid))
                .and(table.materialStage.eq(materialStage))
                .and(table.enabled.eq(1));
        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.id.desc())
                .limit(1);

        return getMapper().selectOneByQuery(wrapper);
    }

    @Override
    public Page<XcxStudentLearningProgressDto> pageListLearning(PageUtil pageUtil, String agencyUid, String storeUid, String studentUid, String projectUid) {
        QueryCondition condition = QueryCondition.createEmpty()
                .and(table.agencyUid.eq(agencyUid))
                .and(table.storeUid.eq(storeUid))
                .and(table.studentUid.eq(studentUid));
        if (StringUtils.isNotBlank(projectUid)){
            condition.and(table.projectUid.eq(projectUid));
        }
        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.id.desc());
        return getMapper().paginateWithRelationsAs(pageUtil.getPage(), pageUtil.getRows(), wrapper, XcxStudentLearningProgressDto.class);
    }

    @Override
    public boolean updateByHomeworkUid(XcxStudentHomework homework, String versionUid) {
        QueryCondition condition = QueryCondition.createEmpty()
                .and(table.homeworkUid.eq(homework.getUid()))
                .and(table.studentUid.eq(homework.getStudentUid()))
                .and(table.versionUid.eq(homework.getVersionUid()))
                .and(table.enabled.eq(1));
        long l = getMapper().selectCountByCondition(condition);
        if (l == 0) return true;
        return UpdateChain.of(XcxStudentLearningProgress.class)
                .set(table.versionUid, versionUid)
                .set(table.lastStaffUid, homework.getLastStaffUid())
                .set(table.lastStaffName, homework.getLastStaffName())
                .where(condition)
                .update();
    }
}
